package workflow

import (
	"encoding/json"
	"project-info/src/handle/common"
	"project-info/src/model"
	"time"
)

type BasicInfo struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

type FormFieldOption struct {
	Label string `json:"label"`
	Value string `json:"value"`
}

type FormFieldValidation struct {
	Rule    string `json:"rule"`
	Message string `json:"message"`
}

type FormField struct {
	Id           string                 `json:"id"`
	Type         string                 `json:"type"`
	Name         string                 `json:"name"`
	Label        string                 `json:"label"`
	Placeholder  string                 `json:"placeholder"`
	Required     bool                   `json:"required"`
	DefaultValue string                 `json:"defaultValue"`
	Options      []*FormFieldOption     `json:"options"`
	Validation   []*FormFieldValidation `json:"validation"`
	Props        map[string]interface{} `json:"props"`
}

type FormConfig struct {
	FormField      []*FormField           `json:"fields"`
	FormLayout     map[string]interface{} `json:"layout"`
	FormValidation map[string]interface{} `json:"validation"`
}

type Metadata struct {
	Version    string `json:"version"`
	ExportTime string `json:"exportTime"`
}

type WorkflowReq struct {
	Id         uint64          `json:"id"`
	BasicInfo  BasicInfo       `json:"basicInfo"`
	FormConfig json.RawMessage `json:"formConfig"`
	FlowConfig json.RawMessage `json:"flowConfig"`
	Metadata   json.RawMessage `json:"metadata"`
}

type WorkflowPageReq struct {
	common.PageRequest
	Name string `json:"name"`
}

type WorkflowPageRecord struct {
	Id         uint64          `json:"id"`
	BasicInfo  BasicInfo       `json:"basicInfo"`
	FormConfig json.RawMessage `json:"formConfig"`
	FlowConfig json.RawMessage `json:"flowConfig"`
	Metadata   json.RawMessage `json:"metadata"`
	CreateTime *time.Time      `json:"createTime"`
	UpdateTime *time.Time      `json:"updateTime"`
}

type NodeUser struct {
	Type     int8   `json:"type"`
	TargetId uint64 `json:"targetId"`
	Name     string `json:"name"`
}

type Node struct {
	WorkflowInstanceId uint64      `json:"workflowInstanceId"`
	NodeName           string      `json:"nodeName"`
	Type               int         `json:"type"`
	NodeUserList       []*NodeUser `json:"nodeUserList"`
	ExamineMode        int         `json:"examineMode"`
	ChildNode          *Node       `json:"childNode"`
	ConditionNodes     []*Node     `json:"conditionNodes"`
	Condition          string      `json:"condition"`
}

type StartWorkflowReq struct {
	WorkflowId   uint64          `json:"workflowId"`
	WorkflowName string          `json:"workflowName"`
	Priority     int             `json:"priority"`
	Variables    json.RawMessage `json:"variables"`
}

type ApproveWorkflowReq struct {
	NodeId      uint64 `json:"nodeId"`
	ApproveType int    `json:"approveType"` // 1: 同意 2: 拒绝 3: 评论
	Comment     string `json:"comment"`
}

type WorkflowInstanceReq struct {
	Id uint64 `json:"id"`
}

type WorkflowInstanceRes struct {
	WorkflowInstance *model.WorkflowInstance `json:"workflowInstance"`
	Nodes            []model.Node            `json:"nodes"`
}

type SingleNode struct {
	WorkflowInstanceId uint64      `json:"workflowInstanceId"`
	NodeName           string      `json:"nodeName"`
	Type               int         `json:"type"`
	NodeUserList       []*NodeUser `json:"nodeUserList"`
	ExamineMode        int         `json:"examineMode"`
	Condition          string      `json:"condition"`
}

type WorkflowInstanceDetailRes struct {
	WorkflowInstance    model.WorkflowInstance    `json:"workflowInstance"`
	NodeActionHistories []model.NodeActionHistory `json:"nodeActionHistories"`
	// 后续节点
	SubsequentNodes [][]SingleNode `json:"subsequentNodes"`
}

type DeleteWorkflowReq struct {
	Id uint64 `json:"id"`
}

type GetWorkflowListReq struct {
	Name string `json:"name"`
}

type FindPenddingApproveReq struct {
	common.PageRequest
	Creator      string `json:"creator"`
	WorkflowName string `json:"workflowName"`
	Priority     *int   `json:"priority"`
}

type FindApproveByMyselfReq struct {
	common.PageRequest
	Creator           string `json:"creator"`
	WorkflowName      string `json:"workflowName"`
	Priority          *int   `json:"priority"`
	ApprovalStartDate string `json:"approvalStartDate"`
	ApprovalEndDate   string `json:"approvalEndDate"`
	Status            string `json:"status"`
}

// 抄送用户
type CarbonCopyUser struct {
	UserName string `json:"userName"` //姓名
	UserId   uint64 `json:"userId"`   //用户ID
	Source   string `json:"source"`   //来源
	SourceId uint64 `json:"sourceId"` //来源ID
}

// 我发起的工作流请求
type StartByMySelfReq struct {
	common.PageRequest
	WorkflowName    string `json:"workflowName"`
	Status          string `json:"status"`
	Priority        *int   `json:"priority"`
	CreateStartDate string `json:"createStartDate"`
	CreateEndDate   string `json:"createEndDate"`
}

// TransferWorkflowReq 转让节点请求
type TransferWorkflowReq struct {
	WorkflowInstanceId uint64      `json:"workflowInstanceId"`
	NodeUserList       []*NodeUser `json:"nodeUserList"`
	Reason             string      `json:"reason"`
}
